package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ClueAdp;
import com.baihan.angus.app.game.busshow.adps.ClueDialogTitleAdp;
import com.baihan.angus.app.game.common.bean.entity.ClueEntity;
import com.baihan.angus.app.game.common.bean.response.ClueResponse;

import java.util.List;
import java.util.stream.Collectors;

//线索dialog
public class ClueDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle;
    private ImageView ivClose;
    private RecyclerView rvClueTitle, rvClues;
    private LinearLayout llNoData, llTitle;
    private View viewDivider;

    private ClueAdp mClueAdp;

    private ClueDialogTitleAdp mClueDialogTitleAdp;
    private List<ClueResponse.DataX> clueList;
    private List<String> titleList;
    private String title;

    public ClueDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_clue, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        rvClueTitle = dialogView.findViewById(R.id.rvClueTitle);
        ivClose = dialogView.findViewById(R.id.ivClose);
        rvClues = dialogView.findViewById(R.id.rvClues);
        llNoData = dialogView.findViewById(R.id.llNoData);
        llTitle = dialogView.findViewById(R.id.llTitle);
        viewDivider = dialogView.findViewById(R.id.viewDivider);

        mClueDialogTitleAdp = new ClueDialogTitleAdp();
        rvClueTitle.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mClueDialogTitleAdp.bindToRecycleView(rvClueTitle);

        mClueDialogTitleAdp.setOnItemClickListener(new ClueDialogTitleAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mClueDialogTitleAdp.setThisPosition(position);
                mClueDialogTitleAdp.notifyDataSetChanged();
                title = clueList.get(position).getTitle();
                tvTitle.setText(title);
                mClueAdp.clear();
                if (null != clueList.get(position).getData() && clueList.get(position).getData().size() > 0) {
                    mClueAdp.addAll(clueList.get(position).getData());
                    rvClues.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                } else {
                    rvClues.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                }
            }
        });

        mClueAdp = new ClueAdp();
        rvClues.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        rvClues.setAdapter(mClueAdp);
        mClueAdp.bindToRecycleView(rvClues);

        mClueAdp.setOnItemClickListener(new ClueAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (null != mCallback) {
                    mCallback.showClueDetail(position, mClueAdp.getItem(position));
                }
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(List<ClueResponse.DataX> list) {
        this.clueList = list;
        mClueDialogTitleAdp.clear();
        mClueAdp.clear();
        if (null != clueList && clueList.size() > 0) {
            rvClues.setVisibility(View.VISIBLE);
            llTitle.setVisibility(View.VISIBLE);
            viewDivider.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);

            //该方法6.0不兼容
            titleList = clueList.stream().map(ClueResponse.DataX::getTitle).collect(Collectors.toList());
            mClueDialogTitleAdp.addAll(titleList);
            title = clueList.get(0).getTitle();
            tvTitle.setText(title);
            if (null != clueList.get(0).getData() && clueList.get(0).getData().size() > 0) {
                mClueAdp.addAll(clueList.get(0).getData());
                rvClues.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
            } else {
                rvClues.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
            mClueDialogTitleAdp.setThisPosition(0);
        } else {
            rvClues.setVisibility(View.GONE);
            llTitle.setVisibility(View.GONE);
            viewDivider.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
    }

    public void refreshClueOpen(int position) {
        mClueAdp.getItem(position).setClue_type(0);
        mClueAdp.notifyDataSetChanged();
    }

    public interface Callback {
        void showClueDetail(int position, ClueEntity clueData);
    }
}
